package dao;

import java.sql.SQLException;
import java.util.List;

import entity.Comment;
import utils.JdbcUtils;

public class CommentDao {
	// {1}增加
	public void addComment(Comment com) throws SQLException {
		String sql = "insert into comment(cid,uid,gid,comment,ctime)values(?,?,?,?,?)";// ?表示占位符，填充插入占位符时表中有几列就写几
		int row = JdbcUtils.update(sql, com.getCid(),com.getUid(),com.getGid(),com.getComment(),com.getCtime());
		System.out.println("已增加" + row + "行数据");
	}

	// {2}删除
	public void deleteComment(Comment com) throws SQLException {
		String sql = "delete from comment where gid=?";
		int row = JdbcUtils.update(sql, com.getCid());
		System.out.println("已删除" + row + "行数据");
	}

	// {3}修改
	public void updateComment(Comment com) throws SQLException {
		String sql = "update comment a set a.comment=?  where a.cid=?";
		int row = JdbcUtils.update(sql, com.getComment(),com.getCid());
		System.out.println("已修改" + row + "行数据");
	}

	// {4}根据商品ID查询
	public Comment queryById(Comment com) throws SQLException {
		String sql = "select * from comment c where c.gid=?";
		Comment c = JdbcUtils.query(sql, Comment.class, com.getGid());
		return c;
	}

	// {5}查询所有记录
	public List<Comment> queryAll() throws SQLException {
		List<Comment> list = null;
		String sql = "select * from comment c where c.cid <= ?";
		list = JdbcUtils.queryList(sql, Comment.class, 1000);
		return list;
	}
	//{6}通过用户ID和商品ID查询评论
	public Comment queryByUGId(int uid,int gid) throws SQLException {
		String sql = "select * from comment c where c.uid=? and c.gid=?";
		Comment c = JdbcUtils.query(sql, Comment.class,uid,gid);
		return c;
	}
	//{7}通过商品ID查询
	public List<Comment> queryByGId(int gid) throws SQLException {
		List<Comment> list = null;
		String sql = "select * from comment c where c.gid=?";
		list = JdbcUtils.queryList(sql, Comment.class, gid);
		return list;
	}
	//{8}通过用户ID查询
	public List<Comment> queryByUId(int uid) throws SQLException {
		List<Comment> list = null;
		String sql = "select * from comment c where c.uid=?";
		list = JdbcUtils.queryList(sql, Comment.class, uid);
		return list;
	}

}
